package com.xm.xmapp;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.response.bean.RequestPushMessageById;
import com.xm.xmapp.response.bean.ResponsePushMessageById;
import com.xm.xmapp.tools.ScreenUtils;
import com.xm.xmapp.tools.StringTools;

public class JpushDetailsActivity extends BaseActivity implements OnRefreshListener {

	private TextView tv_message_title, tv_message_content, tv_message_time;
	private ImageView iv_img;
	private SwipeRefreshLayout id_swipe_ly;
	private RequestPushMessageById requestBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_jpush_details);
	}

	@Override
	public void initView() {

		tv_message_title = (TextView) findViewById(R.id.tv_message_title);
		tv_message_content = (TextView) findViewById(R.id.tv_message_content);
		tv_message_time = (TextView) findViewById(R.id.tv_message_time);
		iv_img = (ImageView) findViewById(R.id.iv_img);
		id_swipe_ly = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);

	}

	@Override
	public void initData() {

		setMyTitle("消息详情");
		int id = getIntent().getIntExtra("id", 0);
		LayoutParams laParams = (LayoutParams) iv_img.getLayoutParams();
		laParams.width = ScreenUtils.getScreenWidth(this);
		laParams.height = laParams.width * 6 / 11;
		iv_img.setLayoutParams(laParams);
		requestBean = new RequestPushMessageById();
		requestBean.message_id = id;
		getData();

	}

	private void getData() {
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				id_swipe_ly.setRefreshing(false);
				ResponsePushMessageById data = JSON.parseObject(response, ResponsePushMessageById.class);
				tv_message_title.setText(data.msg_push_title);
				tv_message_content.setText(data.msg_push_content);
				tv_message_time.setText(StringTools.getTime(data.msg_push_addtime * 1000, "yyyy-MM-dd HH:mm:ss"));
				if (data.msg_push_cover != null && !data.msg_push_cover.equals("")) {
					ImageLoader.getInstance().displayImage(data.msg_push_cover, iv_img);
				} else {
					iv_img.setVisibility(View.GONE);
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
				id_swipe_ly.setRefreshing(false);
			}
		});
	}

	@Override
	public void initEvent() {
		id_swipe_ly.setOnRefreshListener(this);
	}

	@Override
	public void onRefresh() {
		getData();
	}

}
